<?php
    function dayPL($d) {
        switch($d) {
            case 1: return 'poniedziałek';
            case 2: return 'wtorek';
            case 3: return 'środa';
            case 4: return 'czwartek';
            case 5: return 'piątek';
            case 6: return 'sobota';
            case 7: return 'niedziela';

        }
    }

    function monthPL($m) {
        switch($m) {
            case 1: return 'Sty';
            case 2: return 'Lut';
            case 3: return 'Mar';
            case 4: return 'Kwi';
            case 5: return 'Maj';
            case 6: return 'Cze';
            case 7: return 'Lip';
            case 8: return 'Sie';
            case 9: return 'Wrz';
            case 10: return 'Paź';
            case 11: return 'Lis';
            case 12: return 'Gru';
        }
    }

    $today = time();
    $oneDay = 24 * 60 * 60;

    $week = array();
    $offset = 0; // 0 = today is Monday

    while(date('N', $today+$offset*$oneDay) != 1)
            $offset--;

    for($i = 0; $i < 7; $i++) {
        $week[$i] = $today+($offset+$i)*$oneDay;
    }

    $terminy = Wizyta::model()->getDostepneTerminy($week[0], $model->idLekarz);

    Yii::app()->clientScript->registerScript('main', "
        
            var d = new Date();
            var h = d.getHours();
            var m = parseInt(d.getMinutes()/15);
            $('#terminTbody').scrollTo( $('#hour_'+h+'_'+m), 800 );
    ", CClientScript::POS_READY);

    echo "<div class='tableContainer'><table class='scrollTable'><thead class='fixedHeader'><tr><th class='hour'>";
    foreach($week as $day) {
        echo "<th".($day==$today?" class='today'":"")."><span>".dayPL(date('N', $day))."</span><br />"
             .date('j', $day)." ".monthPL(date('n', $day))." ".date('Y', $day)."</th>";
    }
    echo "<th class='filler'></th></tr></thead><tbody id='terminTbody' class='scrollContent'>";
    for($i = 0; $i < 96; $i++) {
        //echo "<tr><td class='hour'>".(($i-$i%4)/4).":".($i%4==0?"00":$i%4*15)."</td>";
        echo "<tr><td class='hour' id='hour_".(($i-$i%4)/4)."_".($i%4)."'>".(($i%4==0) ? ($i/4).":00" : "")."</td>";
        $j = 0;
        foreach($week as $day) {
            if (isset($terminy[$j][$i])) {
                $class = " reserved";
                foreach ($terminy[$j][$i] as $l) {
                    if ($l == 'W') {
                        $class = " free";
                    }
                }
            } else {
                $class = "";
            }
            echo "<td class='norm".($day==$today?" today":"")
                 .$class."'></td>";
            $j++;
        }
        echo "</tr>";
    }
    echo "</tbody></table></div>";
?>
